package com.zonski.jbots.desktop.editer;

import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 27, 2003
 * Time: 9:27:58 AM
 * To change this template use Options | File Templates.
 */
public abstract class AbstractTool implements Tool
{
    private HashSet listeners;
    private boolean active;
    private JBotsEditerContext context;

    public AbstractTool()
    {
        this.listeners = new HashSet();
    }

    public void addToolListener(ToolListener listener)
    {
        this.listeners.add(listener);
    }

    public void removeToolListener(ToolListener listener)
    {
        this.listeners.remove(listener);
    }

    public JBotsEditerContext getContext()
    {
        return this.context;
    }

    public void setContext(JBotsEditerContext context)
    {
        this.context = context;
    }


    public final void activate()
    {
        if(!this.active)
        {
            this.doActivate();
            this.active = true;
            this.fireToolActivationEvent();
        }
    }

    protected void doActivate()
    {

    }

    public final void deactivate()
    {
        if(this.active)
        {
            this.doDeactivate();
            this.active = false;
            this.fireToolDeactivationEvent();
        }
    }

    protected void doDeactivate()
    {

    }

    public boolean isActive()
    {
        return this.active;
    }

    protected final void fireToolActivationEvent()
    {
        ToolEvent event = new ToolEvent(this, true);
        Iterator iterator = this.listeners.iterator();
        while(iterator.hasNext())
        {
            ToolListener listener = (ToolListener)iterator.next();
            listener.toolActivated(event);
        }
    }

    protected final void fireToolDeactivationEvent()
    {
        ToolEvent event = new ToolEvent(this, false);
        Iterator iterator = this.listeners.iterator();
        while(iterator.hasNext())
        {
            ToolListener listener = (ToolListener)iterator.next();
            listener.toolDeactivated(event);
        }
    }
}
